Accusoft.FSInvoices1.Net
Logging Service

A Log Service is a class that implements ILogService.

This service, when set in the Processor.LogService property, enables log messages to be logged by the Analyze method call of the Processor. Three levels of logging are available and defined in ErrorLevelInfo.

Production A minimal amount of logging is provided.
Development More information about the process is provided, normally useful during development.
Detail All log messages available about the process are provided.


Example - Setting the logging into the processor class
Copy Code
// Setup the logging service
FileLogService loggingService = new FileLogService(@"Log.txt");
loggingService.ErrorLevel = ErrorLevelInfo.Detailed;
loggingService.LoggingEnabled = true;
 
invoiceProcessor.LogService = loggingService;

Logging adds time to the overall processing of images, so use it only when needed.

Any logging to a shared resource to be synchronized to ensure no race condition occurs.


Example - A file based implementation of a log service
Copy Code
/// <summary>
/// Implement an ILogService interface, write log messages to a disk file.
/// </summary>
public class FileLogService : ILogService
{
    /// <summary>
    /// Object used for synchronization. 
    /// </summary>
    private object lockObject = new object();
 
    /// <summary>
    /// file name to hold the log.
    /// </summary>
    private string fileName;
 
    /// <summary>
    /// Construct a file logging service
    /// </summary>
    /// <param name="logFileName">File to write log entries.</param>
    public FileLogService(string logFileName)
    {
        fileName = logFileName;
    }
 
    /// <summary>
    /// Error level of logging
    /// </summary>
    public ErrorLevelInfo ErrorLevel{ get; set;}
    /// <summary>
    /// Log an exception message, the receiver of the message should check the
    /// LoggingEnabled and ErrorLevel
    /// to ensure the message should be output
    /// </summary>
    /// <param name="source">Source of the log message</param>
    /// <param name="messageLevel">Level of the message</param>
    /// <param name="exception">Exception type throw</param>
    public void Log(Type source, ErrorLevelInfo messageLevel, Exception exception)
    {
        Log(source, messageLevel, exception, null);
    }
 
    /// <summary>
    /// Log an exception message, the receiver of the message should
    /// check the LoggingEnabled and ErrorLevel
    /// to ensure the message should be output
    /// </summary>
    /// <param name="source">Source of the log message</param>
    /// <param name="messageLevel">Level of the message</param>
    /// <param name="exception">Exception type throw</param>
    /// <param name="message">Log message</param>
    public void Log(Type source, ErrorLevelInfo messageLevel, Exception exception, string message)
    {
        if (LoggingEnabled && messageLevel <= ErrorLevel)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("Exception:" + exception.Message);
            if (string.IsNullOrWhiteSpace(message) == false)
            {
                sb.AppendLine(message);
            }
            sb.AppendLine(exception.StackTrace.ToString());
            Log(source, messageLevel, sb.ToString());
        }
    }
 
    /// <summary>
    /// Log a message, the receiver of the message should check the LoggingEnabled and ErrorLevel
    /// to ensure the message should be output
    /// </summary>
    /// <param name="source">Source of the log message</param>
    /// <param name="messageLevel">Level of the message</param>
    /// <param name="message">Log Message</param>
    public void Log(Type source, ErrorLevelInfo messageLevel, string message)
    {
        string outMessage = messageLevel.ToString() + " : " + message;
        // Check if logging is enable and error level is ok for writing.
        if (LoggingEnabled && messageLevel <= ErrorLevel)
        {
            // lock so threads don't interfere with each other.
            lock (lockObject)
            {
                // Write the log message.
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(fileName, true))
                {
                    file.WriteLine(outMessage);
                }
            }
        }
    }
 
    /// <summary>
    /// Logging enabled if true
    /// </summary>
    public bool LoggingEnabled { get; set;}
}



 

 


©2014. Accusoft Corporation. All Rights Reserved.

Send Feedback